﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace Benchmarks
{

    public class AssociateVsIndexSuite : BenchmarkSuite{
        public AssociateVsIndexSuite() :
            base( "AssociativeVsIndex",
            new AssociativeData(10),
                  new AssociativeData(1000),
                  new AssociativeData(100000),
            new ListData( 10 ),
            new ListData(1000),
            new ListData( 100000 ),
            new ArrayData(10),
            new ArrayData(1000),
            new ArrayData(100000)) { }
    }

    public class AssociativeData : Benchmark {
        private string m_Category;
        private String m_Name;
        public override string Category { get { return m_Category; } }
        public override string Name { get { return m_Name; } }

        private readonly Dictionary< String, int > m_Lookup;
        private readonly List< int > m_List;

        public AssociativeData( int size ) {
            m_Category = "AssociativeVsIndex";
            m_Name = "Associative " + size;

            m_Lookup = new Dictionary< string, int >();
            m_List = new List< int >();

            for( int i=0 ; i < size ; i++ ) {
                m_Lookup.Add( i.ToString(), i  );
                m_List.Add( i );
            }
        }

        public override void StartTrials() {
        }

        public static String current_string;
        public static int current_int;

        public override void RunOverheadTest(int num_iterations)
        {
            for (int i = 0; i < num_iterations; i++) {
                String key = ( i % m_List.Count ).ToString();
                current_string = key;
            }

        }

        public override void RunTrial(int num_iterations)
        {
            for (int i = 0; i < num_iterations; i++)
            {
                String key = (i % m_List.Count).ToString();
                current_string = key;

                current_int = m_Lookup[ key ];
            }
        }
    }

    public class ArrayData : Benchmark
    {
        private string m_Category;
        private String m_Name;
        public override string Category { get { return m_Category; } }
        public override string Name { get { return m_Name; } }

        private readonly Dictionary<String, int> m_Lookup;
        private readonly int[] m_List;

        public ArrayData(int size)
        {
            m_Category = "AssociativeVsIndex";
            m_Name = "Array " + size;

            m_Lookup = new Dictionary<string, int>();
            m_List = new int[size];

            for (int i = 0; i < size; i++)
            {
                m_Lookup.Add(i.ToString(), i);
                m_List[i] = i;
            }
        }

        public override void StartTrials()
        {
        }

        public static String current_string;
        public static int current_int;

        public override void RunOverheadTest(int num_iterations)
        {
            for (int i = 0; i < num_iterations; i++)
            {
                current_int = i % m_List.Length;
            }
        }

        public override void RunTrial(int num_iterations)
        {
            for (int i = 0; i < num_iterations; i++)
            {
                current_int = m_List[i % m_List.Length];
            }
        }
    }
    public class ListData : Benchmark
    {
        private string m_Category;
        private String m_Name;
        public override string Category { get { return m_Category; } }
        public override string Name { get { return m_Name; } }

        private readonly Dictionary<String, int> m_Lookup;
        private readonly List<int> m_List;

        public ListData(int size)
        {
            m_Category = "AssociativeVsIndex";
            m_Name = "List " + size;

            m_Lookup = new Dictionary<string, int>();
            m_List = new List<int>();

            for (int i = 0; i < size; i++)
            {
                m_Lookup.Add(i.ToString(), i);
                m_List.Add(i);
            }
        }

        public override void StartTrials()
        {
        }

        public static String current_string;
        public static int current_int;

        public override void RunOverheadTest(int num_iterations)
        {
            for (int i = 0; i < num_iterations; i++) {
                current_int = i%m_List.Count;
            }
        }

        public override void RunTrial(int num_iterations)
        {
            for (int i = 0; i < num_iterations; i++)
            {
                current_int = m_List[i % m_List.Count];
            }
        }
    }

}
